<template>
	<view>
		<view class="wrap-container">
			<view class="warp-input">
				<view class="warp-text">事件单号:</view>
				<u-input v-model="wh_repair_num" :type="type" :border="border" :height="height" :disabled="true" />
			</view>
			<view class="warp-input">
				<view class="warp-text"> <span style="color:red">*</span>事件类型:</view>
				<!-- <u-input v-model="eq_name" :type="type" :border="border" :height="height" :disabled="true" /> 
				-->
				<u-input @click="searchType" v-model="typename" :type="type" :border="border" :height="height"
					placeholder="请选择事件类型" />
				<u-select v-model="showTypes" :list="types" :border="border" :height="height"
					@confirm="confirmType"></u-select>
			</view>
			<view class="warp-input">
				<view class="warp-text">所属厂站:</view>
				<!-- <u-input v-model="wh_repair_person" :type="type" :border="border" :height="height" :disabled="true" />
			 -->
				<u-input @click="searchOrigan" v-model="organ_name" :type="type" :border="border" :height="height"
					:disabled="disabled" placeholder="请选择厂站" />
				<u-select v-model="showOrigans" :list="origan" :border="border" :height="height"
					@confirm="confirmOrigan"></u-select>
			</view>
			<view class="warp-input">
				<view class="warp-text"> <span style="color:red">*</span>关联位置:</view>
				<!-- <u-input v-model="maintenance_time" :type="type" :border="border" :height="height" :disabled="true" />
			 -->
				<u-input @click="searchPosition" v-model="position_name" :type="type" :border="border" :height="height"
					placeholder="请选择关联位置" />
				<u-select v-model="showPositions" :list="position" :border="border" :height="height"
					@confirm="confirmPosition"></u-select>

			</view>
			<view class="warp-input" @click="searchState">
				<view class="warp-text">设备状态:</view>
				<!-- <select mode="single-column" :show-action="false" shape="square" v-model="wh_equipment_status"
				placeholder="请选择状态"></select> -->
				<u-input @click="searchState" v-model="statusname" :type="type" :border="border" :height="height"
					placeholder="请选择设备状态" />
				<u-select v-model="showStates" :list="states" :border="border" :height="height"
					@confirm="confirmState"></u-select>
			</view>

			<view class="warp-input">
				<view class="warp-text"> <span style="color:red">*</span>事件描述:</view>
				<u-input v-model="wh_declare_description" :type="type" :border="border" :height="height"
					:disabled="false" />
			</view>
			<view class="warp-input">
				<view class="warp-text"> <span style="color:red">*</span>执行人:</view>
				<!-- <u-input v-model="wh_repair_result" :type="type" :border="border" :height="height" :disabled="false" />
			 -->
				<u-input @click="searchRepairPeople" v-model="wh_repair_person_name" :type="type" :border="border"
					:height="height" placeholder="请选择执行人" />
				<u-select v-model="showRepairPeoples" :list="repairPerson" :border="border" :height="height"
					@confirm="confirmRepairPeople"></u-select>
			</view>
			<view class="warp-input"
				v-if="wh_declare_address!=null && wh_declare_address!=''&& wh_declare_address!='null'">
				<view class="warp-text">图片:</view>
				<!-- <u-upload src="" width="350" height="350" maxCount="1"></u-upload> -->
				<!-- 	<img :src="wh_declare_address" style="width: 230px;height:200px;border-radius: 4px;" class="layui-upload-img" id="uploadImg-picture">
			 	 -->
				<img :src="getFullImageUrl()" style="width: 230px;height:200px;border-radius: 4px;"
					class="layui-upload-img" id="uploadImg-picture">

			</view>
			<view class="warp-input" v-else>
				<view class="warp-text special-text">图片:</view>
				<u-upload uploadText="上传图片" @getchild="getchild" :max-size="5242880" width="350" height="350"
					maxCount="1"></u-upload>
			</view>
			<view class="warp-input" v-if="wh_declare_video!=null && wh_declare_video!='' && wh_declare_video!='null'">
				<view class="warp-text special-text">视频:</view>
				<video id="uploadVideo-video" class="layui-upload-img"
					style="width: 230px;height:200px;border-radius: 4px;" controls="controls" :src="getFullVideoUrl()"
					autoplay="autoplay">

				</video>
			</view>
			<view class="warp-input" v-else>
				<view class="warp-text special-text">视频:</view>

				<u-upload-vedio uploadText="上传视频" @getchilds="getchilds" v-model="wh_declare_video"
					:max-size="104857600" :limitType="['mp4', 'jpg', 'jpeg']" width="350" height="350"
					maxCount="1"></u-upload-vedio>

			</view>
			<!--提交按钮-->
			<view class="button" @click="editDeclaration()"><text>确定</text></view>
		</view>
	</view>
</template>
<script>
	import config from '../../../../common/config';
	import Search from '@/components/headsearch/searchFalarm.vue'
	export default {
		components: {
			Search
		},
		props: {
			current: {
				type: Number,
				default: 0
			},
			event: {
				type: Boolean,
				default: false
			}
		},
		data() {
			return {
				imageStyles: {
					width: 64,
					height: 64,
					border: {
						radius: '50%'
					}
				},
				id: "",
				disabled: false,
				type: 'text',
				height: 60,
				border: true,
				organ_uuid: "",
				organ_name: "",
				wh_complete_time: "",
				type_id: "",
				wh_repair_person: "",
				wh_repair_person_name: "",
				wh_repair_result: "",
				wh_repair_num: "",
				eq_name: "",
				eq_id: "",
				is_edit: "",
				wh_declare_time: "",
				wh_equipment_status: "",
				wh_declare_description: "",
				repair_person_id: "",
				wh_repair_status: "",
				maintenance_time: "",
				wh_repair_create_time: "",
				wh_declare_equipment_status: "",
				wh_job_describe: "",
				tags: [],
				statusname: "",
				response_status: "",
				wh_declare_address: "",
				wh_declare_video: "",
				typename: "",
				falarm_id: "",
				states: [{
						value: '2',
						label: '运行'
					},
					{
						value: '3',
						label: '停止'
					},
					{
						value: '1',
						label: '异常'
					},
					{
						value: '4',
						label: '维修维护'
					}

				],
				position_name: '',
				types: [],
				origan: [],
				position: [],
				repairPerson: [],
				showStates: false,
				showTypes: false,
				showOrigans: false,
				showPositions: false,
				showRepairPeoples: false
			}
		},
		// props:{
		// 	event: {
		// 		type: Boolean,
		// 		default: false				
		// 	}
		// },
		created() {
			this.getEqView();
			//获取工单编号
			this.getNewOderNum();
			//获取事件类型
			this.getEventTypes();
			//获取组织厂站
			this.getOgran();
			//获取执行人
			this.getRepairPerson();
		},
		onLoad(e) {
			this.falarm_id = e.falarm_id;
			this.id = e.id;

			var that = this;
			uni.$on('getchild', function(res) {
				//图片
				that.wh_declare_address = res;
				console.log("this.wh_declare_address", config.fileSeePath + that.wh_declare_address)
			});
			uni.$on('getchilds', function(res) {
				//图片
				that.wh_declare_video = res;
				console.log("this.wh_declare_video", config.fileSeePath + that.wh_declare_video)
			});
		},
		onReady() {
			// this.getServerData();
		},
		beforeDestroy() {},
		methods: {
			getFullImageUrl() {
				return config.fileSeePath + this.wh_declare_address;
			},
			getFullVideoUrl() {
				return config.fileSeePath + this.wh_declare_video;
			},
			//查看申报的信息
			getEqView() {
				this.$u.api.eqView({
					"algorithmProcessId": "selectReportById",
					"param": {
						"data": {
							"id": this.id,
							"user_uuid": uni.getStorageSync('uuid')
						}
					}
				}).then(Response => {
					const res = Response.data.data.result.list;
					console.log("===", JSON.stringify(res))
					this.response_status = res.response_status;
					this.wh_declare_num = res.wh_declare_num;
					this.type_id = res.type_id;
					this.wh_repair_num = res.wh_repair_num;
					this.organ_uuid = res.wh_organ_uuid;
					this.eq_id = res.eq_id;
					this.wh_declare_time = res.wh_declare_time;
					this.wh_repair_person_name = res.repair_person;
					this.wh_equipment_status = res.wh_equipment_status;
					this.typename = res.ft_type_desc;
					this.wh_repair_status = res.wh_repair_status;
					this.wh_declare_description = res.wh_declare_description;
					if (res.wh_declare_address != null && res.wh_declare_address != "" && res.wh_declare_address !=
						"null") {
						this.wh_declare_address = res.wh_declare_address;
					}
					this.repair_person_name = res.repair_person;
					this.wh_declare_equipment_status = res.wh_declare_equipment_status;
					if (res.wh_declare_equipment_status == '1') {
						this.statusname = "异常"
					}
					if (res.wh_declare_equipment_status == '2') {
						this.statusname = "运行"
					}
					if (res.wh_declare_equipment_status == '3') {
						this.statusname = "停止"
					}
					if (res.wh_declare_equipment_status == '4') {
						this.statusname = "维修维护"
					}
					this.wh_declare_video = this.wh_declare_video;
					if (res.wh_declare_video != null && res.wh_declare_video != "" && res.wh_declare_video !=
						"null") {
						this.wh_declare_video = res.wh_declare_video;
					}
					this.organ_name = res.organ_name;
					this.position_name = res.organ_name + "-" + res.eq_name;
					this.wh_repair_person = res.repair_person_id;
					this.organ_uuid = res.wh_organ_uuid;
					this.falarm_id = res.falarm_id;
					this.getPosition();
				})
			},
			navTo(url) {
				uni.navigateTo({
					url: url
				});
			},
			confirmState(e) {
				e.map((val, index) => {
					this.statusname = val.label;
					this.wh_declare_equipment_status = val.value;

				})
			},
			searchState() {
				this.showStates = true;
			},
			getchild(e) {
				console.log("====", e)
			},
			getchild1(e) {
				console.log("====", e)
			},
			//事件类型
			confirmType(e) {
				e.map((val, index) => {
					this.typename = val.label;
					this.type_id = val.value;
				})
			},
			searchType() {
				this.showTypes = true;
			},
			//获取新的工单号
			getNewOderNum() {
				this.$u.api.eqView({
					"algorithmProcessId": "querySnowflakesForCode",
					"param": {
						"data": {
							code: 'SJGZ'
						}
					}
				}).then(Response => {
					const res = Response.data.newid;
					this.wh_repair_num = res;
				})
			},
			//获取事件类型
			getEventTypes() {
				this.$u.api.eqView({
					"algorithmProcessId": "getType",
					"param": {
						"data": {}
					}
				}).then(Response => {
					this.types = Response.data.data;
				})
			},
			//厂站
			confirmOrigan(e) {
				e.map((val, index) => {
					this.organ_name = val.label;
					this.organ_uuid = val.value;
				})
				//调用关联位置方法
				this.getPosition();
			},
			searchOrigan() {
				this.showOrigans = true;
			},
			ceshi(data) {
				console.log("数据=======", data)
			},
			//获取厂站
			getOgran() {
				this.$u.api.eqView({
					"algorithmProcessId": "water_organ_user_uuid",
					"param": {
						"data": {
							user_uuid: uni.getStorageSync('uuid')
						}
					}
				}).then(Response => {
					console.log("=========", Response.data.data.result);
					let origan = Response.data.data.result || [];
					if (origan.children.length == 0) {
						//有值-单个厂站
						this.organ_uuid = origan.id;
						this.organ_name = origan.title;
						//禁用该框
						this.disabled = true;
						//调用关联位置方法
						this.getPosition();
					} else {
						//无值-全部厂站
						this.origan = origan.children.map(item => {
							return {
								value: item.id,
								label: item.title
							};
						});
					}
				})
			},
			//关联位置
			confirmPosition(e) {
				e.map((val, index) => {
					this.position_name = val.label;
					this.eq_id = val.value;
				})
			},
			searchPosition() {
				this.showPositions = true;
			},
			//获取关联位置
			getPosition() {
				this.$u.api.eqView({
					"algorithmProcessId": "getStructuresByOrg",
					"param": {
						"data": {
							orgUuid: this.organ_uuid
						}
					}
				}).then(Response => {
					let position = Response.data.data.result || [];
					this.position = position.map(item => {
						return {
							value: item.structureUuid,
							label: item.fullName
						};
					});
				})
			},
			//执行人
			confirmRepairPeople(e) {
				e.map((val, index) => {
					this.wh_repair_person_name = val.label;
					this.wh_repair_person = val.value;
				})
			},
			searchRepairPeople() {
				this.showRepairPeoples = true;
			},
			//获取执行人
			getRepairPerson() {
				this.$u.api.eqView({
					"algorithmProcessId": "getRepairPerson",
					"param": {
						"data": {
							user_uuid: uni.getStorageSync('uuid')
						}
					}
				}).then(Response => {
					let repairPerson = Response.data.data.result || [];
					this.repairPerson = repairPerson.map(item => {
						for (let key in item) {
							return {
								value: key,
								label: item[key]
							};
						}

					});

				})
			},
			//更换图片
			updateHeadPortrait() {
				console.log("测试")
				this.$u.route({
					url: '/uview-ui/components/u-image/u-image',
					params: {
						destWidth: 200, // 输出图片宽高
						rectWidth: 200, // 裁剪框的宽高
						fileType: 'jpg', // 输出的图片类型，如果'png'类型发现裁剪的图片太大，改成"jpg"即可
					}
				})
			},
			//保存
			saveDeclaration(data) {
				console.log("this.wh_declare_video", this.wh_declare_video);
				console.log("this.wh_declare_address：", this.wh_declare_address);
				this.$u.api.eqView({
					"algorithmProcessId": "add_reporting",
					"param": {
						"data": {
							falarm_id: this.falarm_id,
							id: "", //申报表id
							wh_repair_num: this.wh_repair_num, //工单单号
							eq_id: this.eq_id, //设备id
							organ_uuid: this.organ_uuid, //组织id
							type_id: this.type_id, //事件类型id
							user_uuid: uni.getStorageSync('uuid'), //用户id
							wh_declare_address: this.wh_declare_address.replace(
								"http://192.168.110.146:8090/FinforWorx", ""), //图片地址
							wh_declare_description: this.wh_declare_description, //描述
							wh_declare_video: this.wh_declare_video,
							wh_repair_person: this.wh_repair_person, //this.wh_repair_person,
							wh_declare_equipment_status: this.wh_declare_equipment_status
						}
					}
				}).then(Response => {
					const res = Response.code;
					if (res == '200') {
						this.$u.toast("上报成功");

						//成功，跳转至列表页
						if (this.falarm_id) {
							uni.switchTab({
								url: "/pages/sys/home/index"
							});
						} else {
							uni.navigateTo({
								url: "/pages/sys/eventwororder/adddeclaration/adddeclaration"
							});
						}


						// this.Search();
						// this.createReport=false;
						// this.event=true;
					}
				})
			},
			//编辑
			editDeclaration(data) {
				console.log("this.wh_declare_video", this.wh_declare_video);
				console.log("this.wh_declare_address：", this.wh_declare_address);
				this.$u.api.eqView({
					"algorithmProcessId": "update_reporting",
					"param": {
						"data": {
							falarm_id: this.falarm_id,
							id: this.id, //申报表id
							wh_repair_num: this.wh_repair_num, //工单单号
							eq_id: this.eq_id, //设备id
							organ_uuid: this.organ_uuid, //组织id
							type_id: this.type_id, //事件类型id
							user_uuid: uni.getStorageSync('uuid'), //用户id
							wh_declare_address: this.wh_declare_address, //图片地址
							wh_declare_description: this.wh_declare_description, //描述
							wh_declare_video: this.wh_declare_video,
							wh_repair_person: this.wh_repair_person, //this.wh_repair_person,
							wh_declare_equipment_status: this.wh_declare_equipment_status
						}
					}
				}).then(Response => {
					const res = Response.code;
					if (res == '200') {
						this.$u.toast("修改成功");

						//成功，跳转至列表页
						if (this.falarm_id) {
							uni.switchTab({
								url: "/pages/sys/home/index"
							});
						} else {
							uni.navigateTo({
								url: "/pages/sys/eventwororder/eventorder/eventOrder"
							});
						}


						// this.Search();
						// this.createReport=false;
						// this.event=true;
					}
				})
			}
		}
	}
</script>
<style lang="scss">
	@import '../style/common.scss';
</style>